package zuo.from0516.other.day02;

public class Code01_ReverseList {
    public static class Node{
        public int value;
        public Node next;

        public Node(int value) {
            this.value = value;
        }
    }

    public static class DoubleNode{
        public int value;
        public DoubleNode last;
        public DoubleNode next;

        public DoubleNode(int value) {
            this.value = value;
        }
    }

    /**
     * 单链表的反转
     * @param head
     * @return
     */
    public static Node reverseLinkedList(Node head){
        Node pre=null,next=null;
        while(head!=null){
            next=head.next;

            head.next=pre;

            pre=head;
            head=next;
        }
        return pre;
    }

    /**
     * 双向链表的反转
     * @param head
     * @return
     */
    public static DoubleNode reverseDoubleList(DoubleNode head){
        DoubleNode pre=null;
        DoubleNode next=null;
        while(head!=null){
            next=head.next;

            head.next=pre;
            head.last=next;

            pre=head;
            head=next;
        }
        return pre;
    }

    public static Node removeValue(Node head,int num){
        //找到第一个不需要删除的位置,前面的可能有num
        while(head!=null){
            if(head.value!=num){
                break;
            }
            head=head.next;
        }
        Node pre=head;
        Node cur=head;
        while(cur!=null){
            if(cur.value==num){
                pre.next=cur.next;
            }else{
                pre=cur;
            }
            cur=cur.next;
        }
        return head;
    }

}
